package com.xxx.sso.demo;

import com.google.common.collect.Maps;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author caiwl
 * @date 2020/6/1 10:31
 */
public class HashMapSizeDemo {
    public static void main(String[] args) {
        Map<String, String> jdkMap = new HashMap<>(7);
        reflect(jdkMap);

        Map<String, String> guavaMap = Maps.newHashMapWithExpectedSize(7);
        reflect(guavaMap);
    }

    private static void reflect(Map<String, String> jdkMap) {
        try {
            Class<?> mapType = jdkMap.getClass();
            Method capacity = mapType.getDeclaredMethod("capacity");
            capacity.setAccessible(true);
            System.out.println("capacity : " + capacity.invoke(jdkMap));
            Field size = mapType.getDeclaredField("size");
            size.setAccessible(true);
            System.out.println("size : " + size.get(jdkMap));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
